'''
功能：
给两个参数：1、host 2、提取的信息链接
结果：入库。

'''
from pprint import pprint
import requests
from mysql_api import InsertChapter, InsertBook
from setting import spider_api_host

def insert_info(url, host, tag):
    '''
    url:信息链接  
    host:域名
    tag:固定两个参数，book, chapter，用来区分是书还章节
    '''

    params = {'host': host,  "url": url}

    if tag == 'book':

        url = f'http://{spider_api_host}:5000/api/book_info/'

        insertClass = InsertBook

    elif tag == 'chapter':

        url = f'http://{spider_api_host}:5000/api/chapter/'

        insertClass = InsertChapter


    r = requests.get(url, params = params)#向接口发起请求

    info = r.json()#获取接口信息，等同于下面注释的两句话

    print('请求接口数据成功')

    # info = r.text
    # info = json.loads(info)

    insertClass(info)

    print(f'插入成功:{url}')





if __name__ == '__main__':
    
    host = 'http://www.x23us.us'
    url = 'https://www.x23us.us/3_3212/27782202.html'
    tag = 'chapter'
    # url = 'http://www.x23us.us/3_3212/'
    # tag = 'book'
    insert_info(url, host, tag)
    











